﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DTO = Microsoft.MSE.Catalog.Contracts;
using System.Xml.Schema;

namespace Microsoft.MSE.Catalog.Module.Data
{
    public class SchemaImport 
    {
        public SchemaImport()
        {

        }

        public static Guid CreateSchemaImport(Guid schemaID, DTO.SchemaImport import, SqlTransaction txn, SqlConnection conn)
        {
            bool autoclose = false;
            const string sprocname = "[ServiceEngine].[CreateSchemaImport]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter SchemaIDParam = new SqlParameter("@SchemaID", SqlDbType.UniqueIdentifier);
                    SqlParameter NameParam = new SqlParameter("@ImportNamespace", SqlDbType.NVarChar, 256);
                    SqlParameter IDParam = new SqlParameter("@ID", SqlDbType.UniqueIdentifier);

                    cmd.CommandType = CommandType.StoredProcedure;
                    IDParam.Direction = ParameterDirection.Output;
                    NameParam.Value = import.NamespaceURI;
                    SchemaIDParam.Value = schemaID;

                    cmd.Parameters.Add(IDParam);
                    cmd.Parameters.Add(NameParam);
                    cmd.Parameters.Add(SchemaIDParam);

                    cmd.ExecuteNonQuery();
                    import.ID = (Guid)IDParam.Value;
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
            }

            return import.ID;
        }

        public static void UpdateSchemaImport(Guid schemaID, DTO.SchemaImport import, SqlTransaction txn, SqlConnection conn)
        {
            bool autoclose = false;
            const string sprocname = "[ServiceEngine].[UpdateSchemaImport]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter SchemaImportID = new SqlParameter("@SchemaImportID", SqlDbType.UniqueIdentifier);
                    SqlParameter NameParam = new SqlParameter("@ImportNamespace", SqlDbType.NVarChar, 256);

                    cmd.CommandType = CommandType.StoredProcedure;
                    NameParam.Value = import.NamespaceURI;
                    SchemaImportID.Value = import.ID;

                    cmd.Parameters.Add(NameParam);
                    cmd.Parameters.Add(SchemaImportID);

                    cmd.ExecuteNonQuery();
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
            }
        }

        public static void DeleteSchemaImport(Guid schemaID, SqlTransaction txn, SqlConnection conn)
        {
            bool autoclose = false;
            const string sprocname = "[ServiceEngine].[DeleteSchemaImport]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter SchemaID = new SqlParameter("@SchemaID", SqlDbType.UniqueIdentifier);

                    cmd.CommandType = CommandType.StoredProcedure;
                    SchemaID.Value = schemaID;

                    cmd.Parameters.Add(SchemaID);

                    cmd.ExecuteNonQuery();
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
            }
        }

    }
}
